<?php

namespace app\common\validate\shop;

use app\common\model\shop\Goods;
use app\common\validate\BaseValidate;

class OrderValidate extends BaseValidate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'name|姓名' => 'require|length:2,200',
        'mobile|电话' => 'require|isMobile',
        'company_text|牛场名称' => 'require|length:2,200',
        'goods_id|牛ID' => 'require|checkGoods',
        'address_id|地址' => 'require|number',
        'num|数量' =>'require|number|egt:1'
    ];

    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];


    protected function checkGoods($value,$rule,$data){
        $info = Goods::get($value);
        if (!$info) {
            return '商品ID传递错误';
        }
        if ($data['use_time'] > 0) {
            return true;
        }
        if ($info['stock'] < $data['num'] || $info['stock'] <= 0) {
            return '库存不足';
        }

        //判断商品库存
        return true;
    }
    
}
